import { message, Tree } from 'antd';
import React, { useEffect, useState } from 'react';

// import '../css/uintTree.css'

const UintTree = ({ unitData, checkData, setCheckData, addVisible, treeData,selectDisabled }) => {

    const [expandedKeys, setExpandedKeys] = useState([]);
    const [selectedKeys, setSelectedKeys] = useState([]);
    const [checkedKeys, setCheckedKeys] = useState(null)
    const [autoExpandParent, setAutoExpandParent] = useState(true);
    const [style, setStyle] = useState({})

    //默认选中节点
    // useEffect(() => {
        // setCheckData(unitData.owneObjectList);
        // setCheckedKeys(unitData.owneCodeList);
    // }, [unitData]);

    //展开事件
    const onExpand = (expandedKeysValue, { expanded, node }) => {
        setExpandedKeys(expandedKeysValue);
        // setAutoExpandParent(false);
        // if(expanded){
        //   setStyle({overflowY:'auto',height:300})
        // }else{
        //   setStyle({}) 
        // }

    };

    //勾选事件
    const onCheck = (checkedKeysValue, info) => {
        // 保持单选
        setCheckedKeys(checkedKeysValue.checked.length > 1 ? [checkedKeysValue.checked[checkedKeysValue.checked.length - 1]] : checkedKeysValue.checked)

        setCheckData(info.checkedNodes.length > 1 ? [info.checkedNodes[info.checkedNodes.length - 1]] : info.checkedNodes);
    };

    //选择事件
    const onSelect = (selectedKeysValue) => {
        console.log('选择事件')
        setSelectedKeys(selectedKeysValue);
    };

    useEffect(() => {
        if (treeData.length === 0) {
            // message.warning(2222222222)
            setExpandedKeys([]) //关闭树
            setCheckedKeys([])
        } else {
            setCheckedKeys(treeData) //选中树

            setTimeout(() => {
                // message.warning(1111111)
                console.log(treeData)
                setExpandedKeys(treeData)
            }, 1000);
        }
    }, [treeData])

    return (
        <Tree
            height={300}
            checkable
            onExpand={onExpand}
            onCheck={onCheck}
            expandedKeys={expandedKeys}
            checkedKeys={checkedKeys}
            onSelect={onSelect}
            selectedKeys={selectedKeys}
            // treeData={unitData}
            checkStrictly
            rootClassName={'uintTree'}
            treeData={unitData}
            disabled={selectDisabled}
        // style={style}
        />
    );
};

export default UintTree;